# -*- coding: utf-8 -*-
"""
扩展版补充函数
@author:   XueJunJie
@file:     BrowProFunction.py
@date:     2024-9-20
@version:  0.1.1.1
"""
from selenium.webdriver.chrome.webdriver import WebDriver


def setting_popups(driver: WebDriver, flag: bool = True) -> bool:
    """
    设置重定向和弹窗（通过端口控制的浏览器用此方法设置）
    【默认关闭，关闭状态下无法打开新标签页】
    :flag              bool           开启配置
    """
    driver.get("chrome://settings/content/popups")
    if flag:
        key = "enabledRadioOption"
    else:
        key = "disabledRadioOption"
    res = driver.execute_script(
        f"""elm1 = document.querySelector("settings-ui").shadowRoot.querySelector("settings-main").shadowRoot.querySelector("settings-basic-page").shadowRoot;
        elm2 = elm1.querySelector("settings-privacy-page").shadowRoot.querySelector("settings-category-default-radio-group").shadowRoot;
        elm2.querySelector("settings-collapse-radio-button[id={key}]").click();
        return true;"""
    )
    if res:
        driver.back()
        return True
    else:
        return False
